<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind">
      <uni-forms-item name="school_id" label="所属学校" required>
        <uni-data-picker v-model="formData.school_id" collection="admin-school-list" field="school_name as text, _id as value"></uni-data-picker>
      </uni-forms-item>
      <uni-forms-item name="pack_type" label="物件类型" required>
        <uni-data-checkbox v-model="formData.pack_type" :localdata="formOptions.pack_type_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="pack_title" label="物件标题" required>
        <uni-easyinput placeholder="请输入物件的标题信息" v-model="formData.pack_title" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="pack_desc" label="物件描述" required>
        <uni-easyinput type="textarea" placeholder="请输入物件的标题对应的描述信息" v-model="formData.pack_desc" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="moeny" label="标准收费" required>
        <uni-easyinput placeholder="请输入该物品分类的收费标准，xxx元/件，不含加急费用" type="number" v-model="formData.moeny"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="add_money" label="加急收费" required>
        <uni-easyinput placeholder="加急收费标准，xxx元/件，对标准收费以外额外增加的费用" type="number" v-model="formData.add_money"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="enable" label="启用或关闭">
        <switch style="transform: scale(0.8);" @change="binddata('enable', $event.detail.value)" :checked="formData.enable"></switch>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/admin-pack-categories.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'admin-pack-categories';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "school_id": "",
        "pack_type": 0,
        "pack_title": "",
        "pack_desc": "",
        "moeny": null,
        "add_money": null,
        "enable": true
      }
      return {
        formData,
        formOptions: {
          "pack_type_localdata": [
            {
              "text": "小物件",
              "value": 0
            },
            {
              "text": "中物件",
              "value": 1
            },
            {
              "text": "大物件",
              "value": 2
            },
            {
              "text": "超大物件",
              "value": 3
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).add(value).then((res) => {
          uni.showToast({
            title: '新增成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      }
    }
  }
</script>
